package com.dnf.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.jsoup.nodes.Document;

import com.dnf.bean.Goods;
import com.dnf.crawler.GoodsPriceCrawler;
import com.dnf.dao.impl.GoodsDaoImpl;
import com.dnf.util.DateUtil;
import com.dnf.util.MyBatisUtil;
import com.dnf.util.UrlCodeUtil;

/**
 * @author Administrator
 *
 */
public class GoodsPriceService {
public List<Goods> getPriceFromWeb(String goodsName,int diffMonth)
{
	String encodedName = UrlCodeUtil.encodeUrl(goodsName);
	String diffDate = DateUtil.getDateDiffMonth(diffMonth);
	String start = UrlCodeUtil.encodeUrl(DateUtil.formateEncodeDate(diffDate));
	String end = UrlCodeUtil.encodeUrl(DateUtil.formateEncodeToday());
	String url="http://dnf.yxwujia.com/search/search_right_proprice1.aspx?name="+encodedName+"&areaname=%bf%e74%a3%a8%ba%cf%c7%f8%ba%f3%d0%c2%ca%fd%be%dd%a3%a9&serversname=%bf%e74&cxpjjx=&cxgjx=&qtckx=&firstday="+start+"&unit=yxb&cxdjx=djx&ID=2817&rnd=0.0683096751764023&cxzqx=rx&lastday="+end+"&mode=txtlist&serversid=000300330001&areaid=00030033&op=modify"; 	
	Document doc = GoodsPriceCrawler.getDoc(url);
	List<Goods> goodsList = GoodsPriceCrawler.getGoodsList(doc,goodsName);
	GoodsDaoImpl goodsDaoImpl = new GoodsDaoImpl();
	SqlSession session = MyBatisUtil.getSqlSession();
	for(Goods goods:goodsList)	
		   if(goodsDaoImpl.addGoods(session, goods)>0)
			   System.out.println(goods+" inserted "+new Date());
		   else
			   System.out.println(goods+" exists "+new Date());
	MyBatisUtil.closeSession(session);
	return goodsList;
}
}
